<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../assets/module/admin.css?v=315"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">角&emsp;色：</label>
                        <div class="layui-input-inline mr0">
                            <select name="role">
                                <option value="">选择角色</option>
                                <option value="admin">管理员</option>
                                <option value="user">普通用户</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>
<script type="text/html" id="sex">
    {{d.gender==1?'男':'女'}}
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelUser">
    <form id="modelUserForm" lay-filter="modelUserForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <input name="password" type="hidden"/>
        <input name="permission" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入编码" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入电话" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="phone" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-block">
                <input name="address" placeholder="请输入地址" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="1" title="男" checked/>
                <input type="radio" name="gender" value="2" title="女"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role" lay-verType="tips" lay-verify="required">
                    <option value="">请选择角色</option>
                    <option value="admin">管理员</option>
                    <option value="user">普通用户</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitUser" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../assets/js/common.js?v=315"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '/api/getAllUsers',
            page: true,
            height: "full-100",
            toolbar: true,
            headers : {
                Authorization: '[[${session.token}]]'
            },
            cellMinWidth: 100,
            cols: [
                [
                {type: 'numbers', title: '#'},
                {field: 'code', sort: true, title: '编码'},
                {field: 'username', sort: true, title: '用户名'},
                {field: 'gender', sort: true,templet: '#sex', title: '性别'},
                {field: 'phone', sort: true, title: '电话'},
                {field: 'address', sort: true, title: '地址'},
                {field: 'role', sort: true, title: '角色'},
                {align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 200}
            ]
            ]
        });

        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({
                    where: data.field,
                    headers : { Authorization: '[[${session.token}]]'},
                    url:'/api/findUser',
                    page : {curr : 1}},
                'data');
        });

        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.username);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(data.id, data.username);
            }
        });

        // 显示表单弹窗
        function showEditModel(mUser) {
            if(mUser==null){
                admin.open({
                    type: 1,
                    title: '添加用户',
                    content: $('#modelUser').html(),
                    success: function (layero, dIndex) {
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                        var url = '/api/addUser';
                        // 回显数据
                        form.val('modelUserForm', mUser);
                        // 表单提交事件
                        form.on('submit(modelSubmitUser)', function (data) {
                            layer.load(2);
                            $.ajax({
                                url:url,
                                type:"POST",
                                data:JSON.stringify(data.field),
                                headers : { Authorization: '[[${session.token}]]'},
                                contentType:"application/json; charset=utf-8",
                                dataType:"json",
                                success: function(res){
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({}, 'data');
                                    } else {
                                        layer.msg("权限不够", {icon: 2});
                                    }
                                },
                                error : function(xhr,textStatus,errorThrown){
                                    if (xhr.status == 401) {
                                        layer.closeAll('loading');
                                        layer.msg("权限不够", {icon: 2});
                                    } else{
                                        // 调用外部的error
                                        error && error(xhr,textStatus,errorThrown);
                                    }
                                }
                            })
                            return false;
                        });
                    }
                });
            }else{
                admin.open({
                    type: 1,
                    title: '修改用户',
                    content: $('#modelUser').html(),
                    success: function (layero, dIndex) {
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                        var url = '/api/updateUser';
                        //mBook && (mBook.roleId = mUser.roles[0].roleId);
                        // 回显数据
                        form.val('modelUserForm', mUser);
                        // 表单提交事件
                        form.on('submit(modelSubmitUser)', function (data) {
                            console.log(JSON.stringify(data.field))
                            layer.load(2);
                            $.ajax({
                                url:url,
                                type:"POST",
                                data:JSON.stringify(data.field),
                                contentType:"application/json; charset=utf-8",
                                headers : { Authorization: '[[${session.token}]]'},
                                dataType:"json",
                                success: function(res){
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({}, 'data');
                                    } else {
                                        layer.msg("权限不够", {icon: 2});
                                    }
                                },
                                error : function(xhr,textStatus,errorThrown){
                                    if (xhr.status == 401) {
                                        layer.closeAll('loading');
                                        layer.msg("权限不够", {icon: 2});
                                    } else{
                                        // 调用外部的error
                                        error && error(xhr,textStatus,errorThrown);
                                    }
                                }
                            })
                            return false;
                        });
                    }
                });
            }
        }

        // 删除
        function doDel(userId, nickName) {
            layer.confirm('确定要删除“' + nickName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.ajax({
                    url:'/api/delUser',
                    type:"GET",
                    data:{
                        id: userId
                    },
                    headers : { Authorization: '[[${session.token}]]'},
                    dataType:"json",
                    success: function(res){
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg("权限不够", {icon: 2});
                        }
                    },
                    error : function(xhr,textStatus,errorThrown){
                        if (xhr.status == 401) {
                            layer.closeAll('loading');
                            layer.msg("权限不够", {icon: 2});
                        } else{
                            // 调用外部的error
                            error && error(xhr,textStatus,errorThrown);
                        }
                    }
                })
            });
        }

        // 重置密码
        function resetPsw(userId, nickName) {
            layer.confirm('确定要重置“' + nickName + '”的登录密码吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.ajax({
                    url:'/api/resetPass',
                    type:"GET",
                    data:{
                        id: userId
                    },
                    headers : { Authorization: '[[${session.token}]]'},
                    dataType:"json",
                    success: function(res){
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg("权限不够", {icon: 2});
                        }
                    },
                    error : function(xhr,textStatus,errorThrown){
                        if (xhr.status == 401) {
                            layer.closeAll('loading');
                            layer.msg("权限不够", {icon: 2});
                        } else{
                            // 调用外部的error
                            error && error(xhr,textStatus,errorThrown);
                        }
                    }
                })
            });
        }

    });
</script>

</body>
</html>